package org.example.lys.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author liyishan
 * @date 2023/8/28 17:34
 * @apiNote
 */

public class TwoDimensionalCode {

    public static void generateQRCode(String content, String imagePath) {
        // 定义二维码的宽高
        int width = 300;
        int height = 300;

        // 创建 BufferedImage 对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 创建 Graphics2D 对象
        Graphics2D g2d = image.createGraphics();

        // 设置背景色
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        // 设置前景色
        g2d.setColor(Color.BLACK);

        // 设置字体
        Font font = new Font("Arial", Font.PLAIN, 12);
        g2d.setFont(font);

        // 绘制二维码
        g2d.drawString(content, 10, 10);

        // 保存二维码
        try {
            ImageIO.write(image, "png", new File(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        generateQRCode("http://bcms.hvyogo.com/#/invoicesList", "/Users/liyishan/Desktop/picture/1.png");

    }
}
